package com.usian.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Maps;
import com.usian.admin.mapper.AdUserMapper;
import com.usian.admin.service.AdUserService;
import com.usian.model.admin.dtos.AdUserDto;
import com.usian.model.admin.pojos.AdUser;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.utils.common.AppJwtUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.List;

/**
 * Auther：DONG on 2021/11/2
 **/
@Service
public class AdUserServiceImpl extends ServiceImpl<AdUserMapper, AdUser> implements AdUserService {

    @Autowired
    private AdUserMapper adUserMapper;

    @Override
    public ResponseResult login(AdUserDto dto) {
        //1.参数校验
        if (null==dto|| StringUtils.isEmpty(dto.getName())||StringUtils.isEmpty(dto.getPassword())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"用户名或者密码不能为空");
        }
        //2.根据name查询用户信息
        LambdaQueryWrapper<AdUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdUser::getName, dto.getName());
        List<AdUser> AdUserlist = this.list(lambdaQueryWrapper);
        if (null==AdUserlist || AdUserlist.size()!=1){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"用户名不存在");
        }
        //3.将用户传来的密码password加上salt，进行md5加密    和用户的密文进行比较
        AdUser adUser = AdUserlist.get(0);
        String mdtPassword = DigestUtils.md5DigestAsHex((dto.getPassword() + adUser.getSalt()).getBytes());
            //判断
        if (!adUser.getPassword().equals(mdtPassword)){
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
        }

        //4.生成token
        String token = AppJwtUtil.getToken(adUser.getId().longValue());
        //5.如果是成功  将user, token返回
        HashMap<String, Object> resultMap = Maps.newHashMap();
            resultMap.put("token",token);
            adUser.setSalt("");
            adUser.setPassword("");
            resultMap.put("user",adUser);



        return ResponseResult.okResult(resultMap);
    }


//    @Override
//    public ResponseResult findByListPage(SensitiveDto dto) {
//        if (null==dto){
//            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
//        }
//        dto.checkParam();
//        LambdaQueryWrapper<AdSensitive> queryWrapper = new LambdaQueryWrapper<>();
//        if (StringUtils.isNotBlank(dto.getName())){
//            queryWrapper.like(AdSensitive::getSensitives,dto.getName());
//        }
//        Page<AdSensitive> sensitivePage = new Page<>(dto.getPage(),dto.getSize());
//        IPage<AdSensitive> adSensitiveResult = this.page(sensitivePage, queryWrapper);
//        if (null==adSensitiveResult ) {
//            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
//
//        }
//        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) adSensitiveResult.getTotal());
//        pageResponseResult.setData(adSensitiveResult.getRecords());
//    return pageResponseResult;
//    }
//
//    @Override
//    public ResponseResult insert(AdSensitive adSensitive) {
//        //1.参数校验
//        if (null==adSensitive){
//            return  ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
//        }
//        //2.参数填充
//        adSensitive.setCreatedTime(new Date());
//        //3.调用接口实现数据库插入
//        boolean saveResult = this.save(adSensitive);
//        if (!saveResult){
//            return  ResponseResult.errorResult(AppHttpCodeEnum.SAVE_FILE);
//        }
//        //4.返回前台
//
//        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
//    }
//
//    @Override
//    public ResponseResult updateAdSensitive(AdSensitive adSensitive) {
//        //1.参数校验
//        if (null==adSensitive||null== adSensitive.getId()){
//            return  ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
//        }
//        //2.填充参数  无
//        //3.调用接口实现修改数据库
//        boolean updateResult = this.updateById(adSensitive);
//        if (!updateResult){
//            return ResponseResult.errorResult(AppHttpCodeEnum.UPDATE_FILE);
//        }
//        //4.返回前台
//        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
//    }
//
//    @Override
//    public ResponseResult delete(Integer id) {
//        //1.参数校验
//        if (null== id){
//            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
//        }
//        AdSensitive adSensitive = this.getById(id);
//        if (null==adSensitive){
//            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
//        }
//
//        //调用接口实现数据库的删除
//        boolean deleteResult = this.removeById(id);
//        if (!deleteResult){
//            return ResponseResult.errorResult(AppHttpCodeEnum.DELETE_FILE);
//        }
//        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
//    }
}
